/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.mbfasttrack.utils;

import java.io.*;
import java.util.*;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 * @see <a href="http://code.hammerpig.com/how-to-read-really-large-files-in-java.html">HammerPig</a>
 */
public class BigFile implements Iterable<String> {

    private BufferedReader _reader;

    /**
     *
     * @param filePath
     * @throws Exception
     */
    public BigFile(String filePath) throws Exception {
        _reader = new BufferedReader(new FileReader(filePath));
    }

    /**
     *
     */
    public void Close() {
        try {
            _reader.close();
        } catch (Exception ex) {
        }
    }

    /**
     *
     * @return
     */
    public Iterator<String> iterator() {
        return new FileIterator();
    }

    private class FileIterator implements Iterator<String> {

        private String _currentLine;

        public boolean hasNext() {
            try {
                _currentLine = _reader.readLine();
            } catch (Exception ex) {
                _currentLine = null;
                ex.printStackTrace();
            }

            return _currentLine != null;
        }

        public String next() {
            return _currentLine;
        }

        public void remove() {
        }
    }
}